#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan memcached Session Package

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_30"
__date__    = "2013/09/01"
__license__ = 'LGPL'

# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python

# memcache
import memcache

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.session import MikanSiteSession

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------
class MikanMemcacheSiteSession(MikanSiteSession):

	def __init__(self, webapp):
		super(MikanMemcacheSiteSession, self).__init__(webapp)

	def get_memcached(self):
		try:
			return self.FMemcached
		except:
			return True

	def setup(self):
		try:
			connect = self.webapp.config.get("sitesession", "sitesession.connect")
			self.FMemcached = memcache.Client([connect])
		except:
			self.FMemcached = None

	def __getitem__(self, index):
		if self.memcached == True:
			self.setup()
		try:
			return self.memcached.get(index)
		except:
			return None

	def __setitem__(self, index, value):
		if self.memcached == True:
			self.setup()
		try:
			self.memcached.flush_all()
			return self.memcached.set(index, value)
		except:
			pass

	# ----------------------------------------------------
	## Property
	memcached = ESiPCProperty(get_memcached, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------
if __name__ == '__main__':
	pass

# ------------------------------------------------------------
